/*
 * 版权所有(c)  1952110078@qq.com  2022 - 2022
 */

package com.ldc.commonlibs.utils;

import android.content.Context;
import android.content.SharedPreferences;

import com.ldc.commonlibs.common.ComponentContext;

import java.util.Set;

/**
 * @author liudc
 * @since 2022/7/10 00:12
 */
public class SpUtil {
    private final SharedPreferences sharedPreferences;
    private final SharedPreferences.Editor editor;
    private static final String SP_NAME = "MusicData";

    public SpUtil() {
        sharedPreferences = ComponentContext.getInstance().getApplication().getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
        editor = sharedPreferences.edit();
    }

    private static class Holder {
        private static final SpUtil instance = new SpUtil();
    }

    private static SpUtil getInstance() {
        return Holder.instance;
    }

    private static SharedPreferences.Editor editor() {
        return getInstance().editor;
    }

    private static SharedPreferences sharedPreferences() {
        return getInstance().sharedPreferences;
    }

    public static void putString(String key, String value) {
        editor().putString(key, value).apply();
    }

    public static void putBoolean(String key, boolean value) {
        editor().putBoolean(key, value).apply();
    }

    public static void putInt(String key, int value) {
        editor().putInt(key, value).apply();
    }

    public static void putFloat(String key, float value) {
        editor().putFloat(key, value).apply();
    }

    public static void putLong(String key, long value) {
        editor().putLong(key, value).apply();
    }

    public static void putStringSet(String key, Set<String> value) {
        editor().putStringSet(key, value).apply();
    }

    public static String getString(String key, String defaultValue) {
        return sharedPreferences().getString(key, defaultValue);
    }

    public static boolean getBoolean(String key, boolean defaultValue) {
        return sharedPreferences().getBoolean(key, defaultValue);
    }

    public static int getInt(String key, int defaultValue) {
        return sharedPreferences().getInt(key, defaultValue);
    }

    public static float getFloat(String key, float defaultValue) {
        return sharedPreferences().getFloat(key, defaultValue);
    }

    public static long getLong(String key, long defaultValue) {
        return sharedPreferences().getLong(key, defaultValue);
    }

    public static Set<String> getStringSet(String key, Set<String> defaultValue) {
        return sharedPreferences().getStringSet(key, defaultValue);
    }

}
